package fr.chrysalis.ui.components.core
{
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.utils.Dictionary;
	
	import fr.chrysalis.ui.UI;
	import fr.chrysalis.ui.assets.IAsset;
	import fr.chrysalis.ui.components.events.ButtonEvent;
	import fr.chrysalis.ui.components.events.FormEvent;
	import fr.chrysalis.ui.components.events.InputEvent;
	import fr.chrysalis.ui.components.events.InputTextEvent;
	import fr.chrysalis.utils.DLinkedList;
	import fr.chrysalis.utils.DLinkedListNode;
	import fr.chrysalis.ui.components.input.Input;
	import fr.chrysalis.ui.components.display.Button;

	public class Form extends Container
	{
		
		/**
		 * Boutton servant de submit 
		 */
		private var fields:DLinkedList;
		
		/**
		 * Boutton servant de submit 
		 */
		private var btnSubmit:Button;
		
		public function Form(asset:IAsset)
		{
			UI.getInstance().addForm(this);
			this.fields = new DLinkedList();
			super(asset);
		}
		
		/**
		 * Ajoute un champs de saisie 
		 * @param btn Boutton à ajouter
		 */
		public function addField(field:Input):void{
			field.addEventListener(InputEvent.FOCUS_IN, handlerFocusIn);
			field.addEventListener(InputEvent.FOCUS_OUT, handlerFocusOut);
			this.fields.push(field);
			this.addChild(field);
		}
		
		/**
		 * Ajoute un boutton submit 
		 * @param btn Boutton à ajouter
		 */
		public function addSubmit(btn:Button):void{
			this.btnSubmit = btn;
			this.addChild(btn);
			this.btnSubmit.addEventListener(ButtonEvent.CLICK, handlerSubmit);
		}		
		
		/**
		 * Handler lors d'un InputTextEvent.FOCUS_IN 
		 * @param evt
		 */
		private function handlerFocusIn(evt:InputEvent):void{
			
			this.container.stage.addEventListener(KeyboardEvent.KEY_DOWN, handlerKeyDown);
			
			var form:DLinkedListNode = UI.getInstance().forms.head;
			var field:DLinkedListNode;
			while (form)
			{
				
				if (form.value != this){
					
					field = form.value.fields.head;
					while (field)
					{
						(field.value as Input).focusDisable();
						field = field.next;
					}
					
				}else{
					
					field = this.fields.head;
					while (field)
					{
						(field.value as Input).focusEnable();
						field = field.next;
					}
					
				}
				form = form.next;
			}
			
		}
		
		/**
		 * Handler lors d'un InputTextEvent.FOCUS_OUT 
		 * @param evt
		 */
		private function handlerFocusOut(evt:InputEvent):void{
			this.container.stage.removeEventListener(KeyboardEvent.KEY_DOWN, handlerKeyDown);
		}
		
		/**
		 * Handler lors d'un InputTextEvent.FOCUS_OUT 
		 * @param evt
		 */
		private function handlerKeyDown(evt:KeyboardEvent):void{
			if(evt.charCode == Keyboard.ENTER){
				this.submit();
			}
		}		
		
		/**
		 * Handler lors d'un ButtonEvent.CLICK 
		 * @param evt
		 */
		private function handlerSubmit(evt:ButtonEvent):void{
			this.submit();
		}
		
		/**
		 * Valide le formulaire 
		 */
		public function submit():void{
			// Submit valide
			this.dispatchEvent(new FormEvent(FormEvent.SUBMIT, this));
		}
		
		/**
		 * Recupère une valeur d'un formulaire
		 */
		public function getValue(name:String):String{
			return this.fields[name].text;
		}
		
		/**
		 * Récupération du MovieClip container 
		 * @return Le MC du component
		 */
		override public function get():MovieClip {
			return super.get();
		}
		
		override public function toString():String {
			return '[Form id : ' + this.id + ']';
		}
	}
}